package org.jef.auth.config;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jef.core.util.RedisUtils;

/**
 * @version 1.0
 * @description:
 * @author: ChengZhi 0336
 * @create: 2024-10-14 18:08
 **/
public class JefWxMaRedisConfigImpl extends JefAbstractWxMaRedisConfigImpl {

    private static final String ACCESS_TOKEN_KEY = "wa:access_token:";

    private String accessTokenKey;

    /**
     * 每个公众号生成独有的存储key.
     */
    @Override
    public void setAppid(String appId) {
        super.setAppid(appId);
        this.accessTokenKey = ACCESS_TOKEN_KEY.concat(appId);
    }

    @Override
    public String getAccessToken() {
        return RedisUtils.get(this.accessTokenKey);
    }

    @Override
    public boolean isAccessTokenExpired() {

        return RedisUtils.getExpire(this.accessTokenKey) < 2;
    }

    @Override
    public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
        RedisUtils.set(this.accessTokenKey,  accessToken, expiresInSeconds - 200);
    }

    @Override
    public void expireAccessToken() {
        RedisUtils.expire(this.accessTokenKey, 0);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
